package com.casic.config;

import com.casic.common.utils.Constant;
import com.casic.modules.activiti.entity.WorkFlowConfig;
import com.casic.modules.activiti.service.factory.ActGroupEntityServiceFactory;
import com.casic.modules.activiti.service.factory.ActUserEntityServiceFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.StrongUuidGenerator;
import org.activiti.rest.common.application.DefaultContentTypeResolver;
import org.activiti.rest.service.api.RestResponseFactory;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuruijie on 2017/2/20.
 * activiti工作流配置
 */
@Configuration
public class Cfg_Activiti {


    @Value("${spring.datasource.driverClassName}")
    private String  driverClassName;

    @Autowired
    ActUserEntityServiceFactory actUserEntityServiceFactory;
    @Autowired
    ActGroupEntityServiceFactory actGroupEntityServiceFactory;
    //流程配置，与spring整合采用SpringProcessEngineConfiguration这个实现
    @Bean
    public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager){
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(dataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate("true");
        if(Constant.ORACLE_DRIVERCLASSNAME.equals(driverClassName)){
            //配置oracle数据库的方言
            processEngineConfiguration.setDatabaseType("oracle");
        }else if(Constant.MYSQL_DRIVERCLASSNAME.equals(driverClassName)){
            //配置mysql数据库的方言
            processEngineConfiguration.setDatabaseType("mysql");
        }else{
            processEngineConfiguration.setDatabaseType("oracle");
        }
        processEngineConfiguration.setJobExecutorActivate(true);

        processEngineConfiguration.setTransactionManager(transactionManager);

        //流程图字体
        processEngineConfiguration.setActivityFontName("宋体");
        processEngineConfiguration.setAnnotationFontName("宋体");
        processEngineConfiguration.setLabelFontName("宋体");

        //自定义用户权限
        List<SessionFactory> list= new ArrayList<SessionFactory>();
        list.add(actUserEntityServiceFactory);
        list.add(actGroupEntityServiceFactory);
        processEngineConfiguration.setCustomSessionFactories(list);

        return processEngineConfiguration;
    }

    //流程引擎，与spring整合使用factoryBean
    @Bean
    public ProcessEngineFactoryBean processEngine(ProcessEngineConfiguration processEngineConfiguration){
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
        return processEngineFactoryBean;
    }

    //八大接口
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine){
        return processEngine.getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine){
        return processEngine.getRuntimeService();
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine){
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine){
        return processEngine.getHistoryService();
    }

    @Bean
    public FormService formService(ProcessEngine processEngine){
        return processEngine.getFormService();
    }

    @Bean
    public IdentityService identityService(ProcessEngine processEngine){
        return processEngine.getIdentityService();
    }

    @Bean
    public ManagementService managementService(ProcessEngine processEngine){
        return processEngine.getManagementService();
    }

    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine){
        return processEngine.getDynamicBpmnService();
    }

    //八大接口 end


//    //单例json对象
//    @Bean
//    public ObjectMapper objectMapper(){
//        return new ObjectMapper();
//    }

    //引擎内部提供的UUID生成器，依赖fastxml的java-uuid-generator模块
    @Bean
    public StrongUuidGenerator uuidGenerator(){
        return new StrongUuidGenerator();
    }

    // 集成REST服务需要的bean
    @Bean
    public RestResponseFactory restResponseFactory(){
        RestResponseFactory restResponseFactory=new RestResponseFactory();
        return restResponseFactory;
    }
    @Bean
    public DefaultContentTypeResolver contentTypeResolver(){
        DefaultContentTypeResolver contentTypeResolver=new DefaultContentTypeResolver();
        return contentTypeResolver;
    }
    @Bean
    public WorkFlowConfig workFlowConfig(){
        WorkFlowConfig workFlowConfig=new WorkFlowConfig();
        workFlowConfig.setWorkFlowHost("127.0.0.1");
        workFlowConfig.setWorkFlowPort("16666");
        return workFlowConfig;
    }
}
